<?php 

namespace App\Models\Base;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class PowerBase extends Model {

    protected $connection='jmft_power';

    protected $redis;

    public function __construct() {
        $this -> redis = Redis::connection('jmft_power');
    }

    /**
     * 根据订单号查询redis订单
     * params1 String $no
     */
    public function getOrderRedis($no) {

        $data = $this -> redis -> hget('CDZCHARGEORDER', $no);
        if($data) {
            return json_decode($data, true);
        } else {
            return [];
        }

    }

    /**
     * 查询所有redis订单
     */
    public function getOrderRedisAll() {

        $data = $this -> redis -> hgetall('CDZCHARGEORDER');
        return $data;

    }

    /**
     * 验证枪口
     * params1 $pile
     * params2 $gun
     */
    public function checkGun($pile, $gun) {

        $info = $this -> redis -> hget('CDZCHARGEORDERPILENO', $pile . '_' . $gun);
        if (!$info) {
            return true;
        }
        return false;

    }

    /**
     * 存入日志
     * params1 string $name
     * params2 array $data
     */
    public function saveLog($name, $data) {

        $info = $this -> redis -> rPush($name, json_encode($data));

    }

    /**
     * curl请求(post)
     * @param1 string $url
     * @param2 array $data
     * @return bool|mixed
     */
    public function httpRequest($url = '', $data = [])
    {   
 
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['token:' . $this -> getToken()]);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //https请求 不验证证书 其实只用这个就可以了
        // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //https请求 不验证HOST
        $result = curl_exec($curl);
        if ($error = curl_error($curl)) {
            return false;
        }
        if (curl_errno($curl) == CURLE_OPERATION_TIMEDOUT) {
            return false;
        }

        curl_close($curl);
        return json_decode($result, true);
    
    }

    /**
     * 获取token
     * @return bool|string
     */
    public function getToken()
    {
        return $this -> redis -> get('REQUEST_TOKEN');
    }

    /**
     * 修改在线订单
     * 
     */
    public function updateOrder($order)
    {
        $order['status'] = 3;
        $this -> redis -> hset('CDZCHARGEORDER', $order['no'], json_encode($order));
    }

    /**
     * 删除在线订单
     * 
     */
    public function delROrder($order_no)
    {
        $this -> redis -> hdel('CDZCHARGEORDER', $order_no);
    }

    
    /**
     * redis CDZUSER
     * 
     */
    public function updateCdzUser($key)
    {
        $this -> redis -> hset('CDZUSER', $key, $key);
    }

    public function test($key, $data) {
        $this -> redis -> lpush($key, json_encode($data));
    }

    /**
     * 收入类型
     * @return array
     */
    public static function getIncomeType()
    {
        return [
            1 => '省代收入',
            2 => '市代收入',
            3 => '县代收入',
            4 => '服务站收入',
            5 => '裂变收入',
            6 => '间推收入',
            7 => '直推收入',
            8 => '自身收入',
        ];
    }

    /**
     * 设置数组的值
     * @param array $arr
     * @param string $value
     */
    public static function arrSet(&$arr = [], $value = '')
    {
        if ($arr) {
            foreach ($arr as &$v) {
                $v = $value;
            }
        }
    }

}